package com.tpp.base.elastic;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tpp.base.feign.ImageFeign;
import com.tpp.base.mapper.CinemaMapper;
import com.tpp.base.mapper.ItemMapper;
import com.tpp.comm.pojo.TpCinema;
import com.tpp.comm.pojo.TpImage;
import com.tpp.comm.pojo.TpItem;
import com.tpp.comm.util.TpConst;
import org.springframework.beans.BeanUtils;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class EsSyncDateTask {

    @Resource
    private RedisTemplate<String,EsSyncTime> redisTemplate;

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Resource
    private ItemMapper itemMapper;

    @Resource
    private ImageFeign imageFeign;

    @Resource
    private CinemaMapper cinemaMapper;

    @Scheduled(initialDelay = 5,fixedDelay = 60*60,timeUnit = TimeUnit.SECONDS)
    public void executeTask(){
        BoundValueOperations<String, EsSyncTime> ops = redisTemplate.boundValueOps(TpConst.Es.ES_SYNC_TIME);
        Boolean syncTimeKey = redisTemplate.hasKey(TpConst.Es.ES_SYNC_TIME);
        LambdaQueryWrapper<TpItem> itemWrapper = null;
        if(!syncTimeKey){
            //不存在，查询tp_item所有数据做创建索引结构，全量索引
            IndexOperations indexOps = elasticsearchRestTemplate.indexOps(ItemSource.class);
            if (indexOps.exists()) {
                indexOps.delete();
            }
            indexOps.createWithMapping();
        }else{
            EsSyncTime esSyncTime = ops.get();
            //存在，查询添加时间或修改时间大于同步时间点地数据做增量索引
            itemWrapper = new LambdaQueryWrapper<>();
            itemWrapper.ge(TpItem::getCreatedTime,esSyncTime.getValue())
                    .or()
                    .ge(TpItem::getUpdatedTime,esSyncTime.getValue());
        }
        List<TpItem> list = itemMapper.selectList(itemWrapper);
        ops.set(new EsSyncTime());
        list.forEach(e->{
            ItemSource itemSource = new ItemSource();
            BeanUtils.copyProperties(e,itemSource);
            TpCinema tpCinema = cinemaMapper.selectById(e.getCinemaId());
            itemSource.setAreaId(tpCinema.getAreaId());
            itemSource.setAreaName(tpCinema.getAreaName());
            itemSource.setAddress(tpCinema.getAddress());
            TpImage image = imageFeign.findImage(e.getId(), 1, 2);
            itemSource.setImgUrl(image.getImgUrl());
            itemSource.setItemTypeId1(e.getItemType1Id());
            itemSource.setItemTypeId2(e.getItemType2Id());
            elasticsearchRestTemplate.save(itemSource);
        });
    }

}
